package puyu.manage.service.source.controller;

import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import puyu.manage.service.common.config.BaseController;
import puyu.manage.service.common.page.TableDataInfo;
import puyu.manage.service.constant.AjaxResult;
import puyu.manage.service.source.entity.SysTSource;
import puyu.manage.service.source.service.SysTSourceListService;
import puyu.manage.service.source.service.SysTSourceService;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import puyu.manage.service.utils.UserConstants;
import puyu.manage.service.utils.tree.Zone;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

import static puyu.manage.service.constant.AjaxResult.error;
import static puyu.manage.service.constant.AjaxResult.success;


/**
 * 字典主表(SysTSource)表控制层
 *
 * @author makejava
 * @since 2024-08-01 16:41:15
 */
@Slf4j
@Api(tags = "字典主表")
@RestController
@RequestMapping("/sysTSource")
public class SysTSourceController extends BaseController {

    @Resource
    private SysTSourceService sysTSourceService;

    @Resource
    private SysTSourceListService sysTSourceListService;

    /**
     * 列表查询
     */
    @ApiOperation("获取列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Map<String,Object> source) {
        startPage();
        List<Map<String,Object>> list = sysTSourceService.selectAll(source);
        return getDataTable(list);
    }


    /**
     * 新增字典主表
     */
    @ApiOperation("新增字典")
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@RequestBody Map<String,Object> source) {
        sysTSourceService.insert(source);
        return success("新增成功");
    }


    /**
     * 修改字典
     */
    @ApiOperation("修改字典")
    @PutMapping("/update")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult update(@RequestBody Map<String,Object> source) {
        sysTSourceService.update(source);
        return success("修改成功");
    }


    /**
     * 删除字典
     */
    @ApiOperation("批量删除字典")
    @DeleteMapping("/deleteById")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delete(@RequestParam(value = "sourceIds") List<String> sourceIds) {
        sysTSourceService.deleteBySourceIds(sourceIds);
        return success("删除成功");
    }

    /**
     * 通过主表CODE查询子表
     **/
    @ApiOperation("通过主表CODE查询子表")
    @PostMapping("/getBySourceCode")
    public TableDataInfo queryById(@RequestBody Map<String,Object> source) {
        startPage();
        List<Map<String,Object>> list = sysTSourceService.getBySourceCode(source);
        return getDataTable(list);
    }

    /**
     * 新增字典子表数据
     **/
    @ApiOperation("新增字典子表数据")
    @PostMapping("/insertSourceList")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertSourceList(@RequestBody Map<String,Object> sourceList) {
        sysTSourceListService.insertSourceList(sourceList);
        return success("新增成功");
    }

    /**
     * 修改字典子表数据
     **/
    @ApiOperation("修改字典子表数据")
    @PutMapping("/updateSourceList")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateSourceList(@RequestBody Map<String,Object> sourceList) {
        sysTSourceListService.updateSourceList(sourceList);
        return success("修改成功");
    }

    /**
     * 删除字典
     */
    @ApiOperation("批量删除字典")
    @DeleteMapping("/deleteByListId")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult deleteByListId(@RequestParam(value = "ids") List<String> ids) {
        sysTSourceListService.deleteByListId(ids);
        return success("删除成功");
    }


}

